//
//  BreakfastViewController.m
//  FoodUIPickerViewDemo
//
//  Created by Justsoso on 16/2/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "BreakfastViewController.h"

@interface BreakfastViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) NSArray *listLeft;
@property (nonatomic,strong) NSArray *listRight;

@end

@implementation BreakfastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.listLeft = @[@"火腿",@"火鸡",@"花生酱",@"金枪鱼沙拉",@"鸡丁沙拉",@"烤牛肉",@"蔬菜"];
    self.listRight = @[@"白面包",@"粗面粉面包",@"黑麦面包",@"酸面包",@"天然有机7谷"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)btnPressed:(id)sender
{
    int sel1 = [self.pickerView selectedRowInComponent:0];
    int sel2 = [self.pickerView selectedRowInComponent:1];
    NSString *name = [NSString stringWithFormat:@"选择%@和%@",self.listLeft[sel1],self.listRight[sel2]];
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:name delegate:self cancelButtonTitle:@"重新选择" otherButtonTitles:@"确定",nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        int sel1 = [self.pickerView selectedRowInComponent:0];
        int sel2 = [self.pickerView selectedRowInComponent:1];
        self.infoLabel.text = [NSString stringWithFormat:@"你的早饭是%@和%@",self.listLeft[sel1],self.listRight[sel2]];
    }
}


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return self.listLeft.count;
    }
    else
    {
        return self.listRight.count;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.listLeft[row];
    }
    else
    {
        return self.listRight[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    int sel1 = [self.pickerView selectedRowInComponent:0];
    int sel2 = [self.pickerView selectedRowInComponent:1];
    
    [self.btnText setTitle:[NSString stringWithFormat:@"选择%@和%@",self.listLeft[sel1],self.listRight[sel2]] forState:UIControlStateNormal];
}
@end
